<template>
  <div class="active">
   <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
    <h3>修改密码</h3>
      <el-form-item label="原密码" prop="password">
        <el-input v-model="pwdForm.password" type="password"  minlength="6" maxlength="15"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="pwdForm.newPassword" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="pwdForm.confirmPassword" type="password"></el-input>
      </el-form-item>
       <el-form-item>
        <el-button type="primary" @click="updatePwd">确认修改</el-button>
       </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {information, login} from '@/api/login'
import {passwordonfig} from '@/api/limits'
import { resetRouter } from "@/router";

export default {
data () {
      const xinewpwd = (rule, value, callback) => {
    if (value === this.pwdForm.password) {
      return callback(new Error('新旧密码不能相同！'))
    }
    callback()
  }
   const rePwd = (rule, value, callback) => {
    if (value !== this.pwdForm.newPassword) {
      return callback(new Error('两次新密码不一致！'))
    }
    callback()
  }

  return {
    lastname:'',
     pwdForm:{
       id:'',
       password:'',
       newPassword:'',
       confirmPassword:'',
     },
        // 表单的验证规则对象
      pwdFormRules: {
        password:[
          {required:true, message:'请输入旧密码', trigger:'blur' },
          {pattern:/^\S{6,15}$/,message:'请输入6~15位的非空字符',trigger:'blur'},
        ],
         newPassword:[
          {required:true, message:'请输入新密码', trigger:'blur' },
          {pattern:/^\S{6,15}$/,message:'请输入6~15位的非空字符',trigger:'blur'},
            { validator: xinewpwd, trigger: 'blur' }
        ],
         confirmPassword:[
          {required:true, message:'请确认新密码', trigger:'blur' },
          {pattern:/^\S{6,15}$/,message:'请输入6~15位的非空字符',trigger:'blur'},
           { validator: rePwd, trigger: 'blur' }
        ]
      }
  }
},
created(){
  this.loachgeeys()
},
methods:{
 async loachgeeys(){
    this.lastname = this.$cookies.get('name')
   const res = await information()
   this.pwdForm.id = res.data.id
  },
  async updatePwd(){
      const resd = await passwordonfig(this.pwdForm)
      if(resd.code == 200){
        this.$message.success(resd.msg)
        console.log(resd);
      this.$refs.pwdFormRef.resetFields()
           this.$cookies.remove('re-token')
          //  sessionStorage.removeItem('name')
           this.$cookies.remove('name');
          console.log(this.lastname)
          if(this.lastname == 'cms'){
            this.$router.push("/cms")
          }else{
            // location.reload();
              this.$router.push("/")
          }
          // 提醒用户
          this.$message.success("密码修改成功,请重新登陆");
          resetRouter();
      } else{
        console.log('123');
        this.$message.error(resd.msg)
        }
  }
}
}
</script>

<style scoped>
.active{
  width: 100%;
  height: 100%;
  background-color: #fff;
  padding-top: 50px;
}
h3{
  text-align: center;
}
.el-form {
  width: 500px;
  margin: 0 auto;
}
.el-button{
  border: 1px solid #ff7144;
  width: 100%;
  font-size: 16px;
  border-radius: 5px;
}
</style>